package edu.odu.cs.sstore.domain;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.jasypt.hibernate.type.EncryptedStringType;

import edu.odu.cs.sstore.dto.DocumentDTO;

@TypeDef(
    name="encryptedString", 
    typeClass=EncryptedStringType.class, 
    parameters= {
        @Parameter(name="encryptorRegisteredName", value="strongHibernateStringEncryptor")
    }
)


@Entity
public class Document implements Serializable {

	
	private static final long serialVersionUID = -7511437271663351509L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;
	
	private String fileName;
	
	@Type(type="encryptedString")
	private String fileContent;
	
	@ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "creatorUserId")
	private User creator;
	
	private Document() {
		this.fileContent = null;
		this.fileName = null;
		this.creator = null;
	}
	
	public Document(String fileName, User creator) {
		this();
		this.fileName = fileName;
		this.creator = creator;
	}

	public Long getId() {
		return id;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public User getCreator() {
		return creator;
	}
	
	public DocumentDTO toDTO() {
		return new DocumentDTO(this.id, this.fileName, this.fileContent, this.creator.toDTO());
	}
	
	public void setFileContent(String content) {
		this.fileContent = content;
	}
	
	public String getFileContent() {
		return this.fileContent;
	}
}
